/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler
 * File: ItemHandler.java
 * Created: Nov 3, 2005 11:37:22 AM
 * Author: Sancza 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler;

import java.io.Serializable;
import java.util.List;

import com.amkai.aes.aescommon.preference.PreferenceManager;
import com.amkai.aes.aescommon.preference.PreferenceResult;
import com.amkai.aes.aescommon.service.binary.job.ItemJob;
import com.amkai.aes.aescommon.service.binary.job.ItemJobKind;
import com.amkai.aes.aescommon.service.result.ItemsResult;
import com.amkai.aes.generatedbeans.organization.Department;
import com.amkai.aes.generatedbeans.preferences.QuickCodeAutoGeneration;
import com.amkai.aes.generatedbeans.scheduling.AllowedRole;
import com.amkai.aes.generatedbeans.scheduling.SchedulingResource;
import com.amkai.aes.server.dictionary.DictionarySchedulingResourceManager;
import com.amkai.aes.server.dictionary.SharedDictionaryManager;
import com.amkai.aes.server.items.ItemsManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class ItemHandler implements
		JobObjectHandler<ItemJob, Serializable, ItemsResult> {

	@SuppressWarnings("unchecked")
	public ItemsResult process(ItemJob job, Serializable object) throws Exception {
		SessionManager sm = SiteManager.Start();
		try {
			// KKORNEL: AES-6959 - JobKinds
			if ((job.getJobKind() == null) || (job.getJobKind() == ItemJobKind.NORMAL) || (job.getJobKind() == ItemJobKind.ITEMS_BY_DEPARTMENT)) {
				// KKORNEL: AES-11533
				if (job.isActivation()) {
					// KKORNEL: AES-4465
					/*DictionaryManager dm = new DictionaryManager(sm);
					BaseOrganization baseOrganization = sm.get(BaseOrganization.class, job.baseOrganizationId);
					dm.activateItemsForBaseOrganzation(baseOrganization);*/
					return null;
				}
				else {
					// KKORNEL: AES-11533
					if((object != null) && (object instanceof Department)) {
						job.setDepartment((Department) object); 
					}
					else {
						Department runtimeDepartment = job.getModuleDepartment();
						job.setDepartment(sm.load(Department.class, runtimeDepartment.getId()));
					}
					return ItemsManager.processJob(job, object);
				}
			// KKORNEL: AES-6959
			} else if ((job.getJobKind() == ItemJobKind.GET_ITEM_IN_CURRENT_DEPARTMENT) ||
					   (job.getJobKind() == ItemJobKind.GET_ITEMS_IN_CURRENT_DEPARTMENT) ||
					   (job.getJobKind() == ItemJobKind.GET_ITEM_ID_IN_CURRENT_DEPARTMENT) ||
					   (job.getJobKind() == ItemJobKind.GET_ITEM_IDS_IN_CURRENT_DEPARTMENT)) {
				SharedDictionaryManager sdm = new SharedDictionaryManager(sm);
				ItemsResult itemsResult = null;
				itemsResult = sdm.processGetItemInCurrentDepartmentJobKinds(job);
				//sm.closeSession();
				return itemsResult;
			// KKORNEL: AES-11533
			} else if (job.getJobKind() == ItemJobKind.GET_ITEM_BY_BE_KIND_AND_SHORTNAME) {
				SharedDictionaryManager sdm = new SharedDictionaryManager(sm);
				ItemsResult itemsResult = new ItemsResult();
				itemsResult.setItem(sdm.getItemForKindBOAndShortName(job.getItemKind(), job.getModuleDepartment(), job.getQuickCode()));
				//sm.closeSession();
				return itemsResult;
			// KKORNEL: AES-8478
			} else if (job.getJobKind() == ItemJobKind.NEED_TO_SHOW_SHAREDANDSYSTEMKIND) {
				List<Long> departmentIds = sm.execute("SELECT id FROM Department").list();
				ItemsResult itemsResult = new ItemsResult();
				itemsResult.setNeedToShowSharedAndSystemKind((departmentIds != null) && (departmentIds.size() > 1));
				return itemsResult;
			// KKORNEL: AES-12742
			} else if (job.getJobKind() == ItemJobKind.CLEAR_ALL_NATIONAL_ASC_RATE_FOR_CPTCODES) {
				sm.openTransaction();
				sm.execute("UPDATE CptCode SET nationalASCRate = null WHERE nationalASCRate is not null").executeUpdate();
				sm.commitTransaction();
				return null;
			// KKORNEL: AES-15040
			} else if (job.getJobKind() == ItemJobKind.ACTIVATE_ITEM_AND_CREATE_AND_RETURN_SCHEDULING_RESOURCE) {
				return ItemsManager.activateItemAndCreateAndReturnSchedulingResurce(sm, job.getItem());
			// KKORNEL: AES-5817
			} else if (job.getJobKind() == ItemJobKind.GENERATE_QUICK_CODE_AUTOMATICALLY) {
				ItemsResult itemsResult = new ItemsResult();
				PreferenceResult<QuickCodeAutoGeneration> result = PreferenceManager.getPreference(QuickCodeAutoGeneration.class, job.getBaseOrganizationId());
				itemsResult.setGeneratedShortName(new SharedDictionaryManager(sm).generateShortName(result.getPreference(), job.getItemID(), job.getItemKind(), job.getBaseOrganizationId(), job.getName()));
				return itemsResult;
			} else if (job.getJobKind() == ItemJobKind.IS_ROOM_USED_BY_APPOINTMENTS) {
				//#18490
				ItemsResult itemsResult = new ItemsResult();
				DictionarySchedulingResourceManager dsrm = new DictionarySchedulingResourceManager(sm);
				boolean isRoomUsedByAppointments = dsrm.isRoomUsedByAppointments(job.getItemID(), job.getBaseOrganizationId());
				itemsResult.setRoomUsedByAppointments(isRoomUsedByAppointments);
				dsrm = null;
				return itemsResult;
			} else if (job.getJobKind() == ItemJobKind.DOWNLOAD_SCHEDULINGRESOURCE_OF_ROOM) {
				//#18490
				ItemsResult itemsResult = new ItemsResult();
				DictionarySchedulingResourceManager dsrm = new DictionarySchedulingResourceManager(sm);
				SchedulingResource schedulingResource = dsrm.downloadSchedulingResourceOfRoom(job.getItemID());
				itemsResult.setSchedulingResource(schedulingResource);
				dsrm = null;
				return itemsResult;
			} else if (job.getJobKind() == ItemJobKind.SAVE_ALLOWEDROLE) {
				//#18490
				ItemsResult itemsResult = new ItemsResult();
				DictionarySchedulingResourceManager dsrm = new DictionarySchedulingResourceManager(sm);
				AllowedRole allowedRole = dsrm.saveAllowedRole(job.getAllowedRole());
				itemsResult.setAllowedRole(allowedRole);
				dsrm = null;
				return itemsResult;
			} else {
				return null;
			}
		}
		finally {
			sm.closeSession();
		}
	}

}
